package jobs;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import models.BuyerTask;
import models.Task;
import models.mappers.BuyerTaskMapper;

import org.apache.ibatis.session.SqlSession;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import play.jobs.Every;

import com.aton.config.BizConstants;
import com.aton.db.SessionFactory;
import com.aton.job.BaseJob;

import enums.CollectionType;

/**
 * 
 * 
 * 系统自动撤销超过规定时间的已接手未支付的买手任务
 * 
 * @author moloch
 * @since v0.1
 * @created 2014-9-23 下午3:10:00
 */
@Every("3min")
public class BuyerTaskCancel extends BaseJob {
	private SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	private static final Logger log = LoggerFactory.getLogger(BuyerTaskCancel.class);

	public void doJob() throws Exception {

		// 操作时间点 这个时间之前的已接手没支付的任务，全部撤销
		Date dateTip = DateTime.now().minusMinutes(BizConstants.BUYER_TASK_AUTO_CANCEL_TIME).toDate();
		//Date dateJHSTip = DateTime.now().minusHours(BizConstants.BUYER_JHSTASK_AUTO_CANCEL_TIME).toDate();
		Date dateJHSTip = DateTime.now().minusMinutes(BizConstants.BUYER_TASK_AUTO_CANCEL_TIME).toDate();
		log.info("cancel buyerTask start");
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try {
			BuyerTaskMapper btMapper = ss.getMapper(BuyerTaskMapper.class);
			List<BuyerTask> btsjhs = btMapper.selectForJHSrCancel(dateJHSTip);
			List<BuyerTask> bts = btMapper.selectForOtherrCancel(dateTip);
			List<BuyerTask> btqcscjg=getScjg();
			bts.addAll(btsjhs);
			bts.addAll(btqcscjg);
			// 循环撤销任务
			for (BuyerTask bt : bts) {
				bt.cancelStep(ss);
				ss.commit();
			}
			log.info("cancel buyerTask end");
		} finally {
			ss.close();
		}
	}
	/**
	 * 若为收藏加购则更改时长
	 * @Title: getScjg 
	 * @Description: TODO
	 * @return
	 * @return: List<BuyerTask>
	 */
	private List<BuyerTask> getScjg() {
		if(1==1){
			return new ArrayList();
		}
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		List<BuyerTask> btqcscjg=new ArrayList<BuyerTask>();
		try {
			BuyerTaskMapper btMapper = ss.getMapper(BuyerTaskMapper.class);
			List<BuyerTask> btsjhs = btMapper.selectForSCJGCancel();
			for(BuyerTask bt : btsjhs){
				Task t=Task.findById(bt.taskId);
				int hour=0;
				if(t.collectionType==CollectionType.ONE){
					hour=-24;
				}
				if(t.collectionType==CollectionType.TWO){
					hour=-48;
				}
				if(t.collectionType==CollectionType.THREE){
					hour=-72;
				}
				//Date dateTip = DateTime.now().minusHours(hour).toDate();
				Date dateTip=getLongDate(hour);
				log.info(hour+"----------time:"+sFormat.format(dateTip)+"------taketime:"+sFormat.format(bt.takeTime));
				if(dateTip.getTime()>=bt.takeTime.getTime()){
					log.info(hour+"----------time:"+sFormat.format(dateTip)+"------taketime:"+sFormat.format(bt.takeTime));
					btqcscjg.add(bt);
				}
			}
		}finally {
			ss.close();
		}
		return btqcscjg;
	}
	/**
	 * 获取当前时间减去收藏加购时长的时间
	 *
	 * @Title: getLongDate 
	 * @param hour 收藏加购时长
	 * @return
	 * @return: Date
	 */
	private Date getLongDate(int hour) {
		// TODO Auto-generated method stub
		Date date=new Date();
		Calendar dar=Calendar.getInstance();
		dar.setTime(date);
		dar.add(Calendar.HOUR, hour);
		return dar.getTime();
	}
}
